using QueryBuilder.Core.Query;

namespace QueryBuilder.Core.Visualization.QueryVisualization
{
    /// <summary>
    /// 
    /// </summary>
    public class VisualizableExpression : VisualizableConstraint
    {
        /// <summary>
        /// 
        /// </summary>
        private string comparison;

        /// <summary>
        /// 
        /// </summary>
        private readonly CreateVisualizableConstraintVisitor createVisualizableConstraint = new CreateVisualizableConstraintVisitor();

        /// <summary>
        /// Initializes a new instance of the <see cref="VisualizableExpression"/> class.
        /// </summary>
        public VisualizableExpression()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VisualizableExpression"/> class.
        /// </summary>
        /// <param name="expression">The expression.</param>
        public VisualizableExpression(Expression expression)
        {
            this.CreateVisualizableExpressionFromModel(expression);
        }

        /// <summary>
        /// Gets or sets the comparison.
        /// </summary>
        /// <value>The comparison.</value>
        public string Comparison
        {
            get { return this.comparison; }
            set { this.SetValue(ref this.comparison, value, () => this.Comparison); }
        }

        /// <summary>
        /// Creates the visualizable expression from model.
        /// </summary>
        /// <param name="expression">The expression.</param>
        private void CreateVisualizableExpressionFromModel(Expression expression)
        {
            this.comparison = expression.Comparison;
            foreach (Constraint constraint in expression.Constraints)
            {
                this.AddChild(this.createVisualizableConstraint.CreateFrom(constraint));
            }
        }

        /// <summary>
        /// Accepts the specified visitor.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        public override void Accept(IVisualizableConstraintVisitor visitor)
        {
            visitor.Visit(this);
        }
    }
}